package itsm.isperp.module.service.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.commons.repository.BaseRepository;
import itsm.isperp.framework.message.domain.MessageType;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.module.entity.app.AppMessage;
import itsm.isperp.module.entity.app.AppMessageReceiver;
import itsm.isperp.module.entity.app.AppMessageSuccess;
import itsm.isperp.module.repository.app.AppMessageReceiverMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppMessageReceiverService extends BaseService<AppMessageReceiver> {

	@Autowired
	protected AppMessageReceiverMapper appMessageReceiverMapper;
	@Autowired
	protected AppMessageService appMessageService;

	@Override
	public BaseRepository<AppMessageReceiver> getRepository() {
		return this.appMessageReceiverMapper;
	}

	public void sendTo(AppMessageReceiver receiver, String title,
			String content, String messageType) {

		AppMessageSuccess message = new AppMessageSuccess();
		message.setContent(content);
		message.setMessageLevel(1);
		message.setSenderId(ContextHolder.getLoginUsername());
		message.setTitle(title);
		message.setMessageType(messageType);
		message.setSourceId(receiver.getSourceId());
		message.setReceiverId(receiver.getReceiverId());
		this.sendTo(receiver, message);
	}

	public void sendTo(AppMessageReceiver receiver, AppMessage message) {

		this.getRepository().save(receiver);
		this.appMessageService.saveOrUpdate(message);
		receiver.setAppMessageId(message.getId());
	}

}
